from ..async_tcp_server import serverInQueue, serverOutQueue
import asyncio


class FanControl:

    def __init__(self):
        self.__openStatus: bool = False
        self.__extraSpeed: str = '慢'

    def getOpenStatus(self):
        return self.__openStatus

    def getExtraSpeed(self):
        return self.__extraSpeed

    async def setOpenStatus(self, status: bool):
        self.__openStatus = status
        cmd = self.__changeCMD()
        await self.__sendCMD(cmd)

    async def setExtraSpeed(self, speed: str):
        self.__extraSpeed = speed
        cmd = self.__changeCMD()
        await self.__sendCMD(cmd)

    def __changeCMD(self):
        signal = '1' if self.__openStatus else '0'
        if self.__extraSpeed == '慢':
            speed = 'S'
        elif self.__extraSpeed == '中':
            speed = 'M'
        else:
            speed = 'F'
        return f"F={signal}{speed};"

    async def __sendCMD(self, cmd):
        serverInQueue.put(cmd)
        res_status = "F"
        while res_status != 'T':
            if not serverOutQueue.empty():
                res_status = serverOutQueue.get()
            else:
                await asyncio.sleep(0)
